package com.bw.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.auth.domain.PoSysUser;
import com.bw.auth.domain.SysUser;
import com.bw.auth.domain.SysUserDept;
import com.bw.auth.domain.SysUserRole;
import com.bw.auth.mapper.SysUserDeptMapper;
import com.bw.auth.mapper.SysUserRoleMapper;
import com.bw.auth.service.SysRoleService;
import com.bw.auth.service.SysUserService;
import com.bw.auth.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 20261
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2024-05-23 11:24:34
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysUserDeptMapper sysUserDeptMapper;
    @Override
    public SysUser getUser(SysUser sysUser) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(sysUser.getUname()!=null&&sysUser.getUname()!="","uname",sysUser.getUname());
        queryWrapper.or();
        queryWrapper.eq(sysUser.getTel()!=null&&sysUser.getTel()!="","tel",sysUser.getTel());
        SysUser sysUser1 = sysUserMapper.selectOne(queryWrapper);
        if (sysUser1!=null){
            //查询角色
           SysUser sysUser2 = sysUserMapper.getUser(sysUser1);
           return sysUser2;
        }else {
            return null;
        }
    }

    @Override
    public List<String> getauth(Integer id) {
        return baseMapper.getauth(id);
    }

    @Override
    public List<PoSysUser> getUserlist(SysUser sysUser) {
        return sysUserMapper.selectLists(sysUser);
    }

    @Override
    @Transactional
    public boolean addsysUser(SysUser sysUser, Integer rid, Integer did) {
        //先添加用户
        sysUserMapper.insert(sysUser);
        //添加用户角色关系表
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getId());
        sysUserRole.setRoleId(rid);
        sysUserRoleMapper.insert(sysUserRole);

        //添加用户部门关系表.
        SysUserDept sysUserDept = new SysUserDept();
        sysUserDept.setDeptId(did);
        sysUserDept.setUserId(sysUser.getId());
        sysUserDeptMapper.insert(sysUserDept);

        return true;
    }

    @Override
    public List<SysUser> getUserRole() {
        return sysUserMapper.getUserRole();
    }
}




